package com.fanfanwang.ffw.app.asynctask;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;

import org.json.JSONException;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.bean.SucUserBean;
import com.fanfanwang.ffw.app.bean.UserBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.LogUtil;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.HomeActivity;
import com.fanfanwang.ffw.app.ui.login.LoginAct;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;


public class TenantLoginAsyncTask extends IssAsyncTask<String, String, SucUserBean> {
	private Activity mActivity;
	private String mobile;
	private String password;
	public TenantLoginAsyncTask(Activity activity,String mobile,String password) {
		super(activity);
		mActivity = activity;
		this.mobile = mobile;
		this.password = password;
	}

	@Override
	protected SucUserBean doInBackground(String... params) {
		// TODO 	addAuctionPrice($apiusername,$apipassword,$userid,$pid,$price)
		SucUserBean sucUserBean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("username", mobile);
		paramsMap.put("password",  Md5Util.getMD5To32(password));
		try {
			sucUserBean = IssNetLib.getInstance(mActivity).getLogin(WebServiceHelper._TENANT_LOGIN, paramsMap);
			
		} catch (HttpRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//			exception = getResources().getString(R.string.e_net_error);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//			exception = getResources().getString(R.string.e_json);
		}
		return sucUserBean;
	}

	@Override
	protected void onPostExecute(SucUserBean result) {
		super.onPostExecute(result);
		if (exception != null) {
			ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {

				if(result.data != null){
					try {
						if(!TextUtils.isEmpty(result.data.parkingid)){
							String user_id;
							user_id = URLDecoder.decode(result.data.parkingid,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putParkingId(user_id);
						}
						if(!TextUtils.isEmpty(result.data.transactor)){
							String transactor;
							transactor = URLDecoder.decode(result.data.transactor,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putTransactor(transactor);
						}
						if(!TextUtils.isEmpty(result.data.pianqu)){
							String pianqu;
							pianqu = URLDecoder.decode(result.data.pianqu,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putPianqu(pianqu);

						}
						if(!TextUtils.isEmpty(result.data.chargetype)){
							String chargetype;
							chargetype = URLDecoder.decode(result.data.chargetype,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putChargetype(chargetype);
						}
						if(!TextUtils.isEmpty(result.data.chargeprice)){
							String chargeprice;
							chargeprice = URLDecoder.decode(result.data.chargeprice,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putChargeprice(chargeprice);
						}
						if(!TextUtils.isEmpty(result.data.opentime)){
							String opentime;
							opentime = URLDecoder.decode(result.data.opentime,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putOpentime(opentime);
						}
						if(!TextUtils.isEmpty(result.data.parktype)){
							String parktype;
							parktype = URLDecoder.decode(result.data.parktype,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putParktype(parktype);
						}
						if(!TextUtils.isEmpty(result.data.name)){
							String name;
							name = URLDecoder.decode(result.data.name,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putName(name);
						}
						if(!TextUtils.isEmpty(result.data.address)){
							String address;
							address = URLDecoder.decode(result.data.address,"utf-8");
							SharedPreferencesUtil.getInstance(mActivity).putAddress(address);
						}
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					Intent intent =new Intent(mActivity,HomeActivity.class);
					//					intent.putExtra("data", result.data);
					mActivity.startActivity(intent);

				}
			}else if(result.status == 0){

				if (!TextUtils.isEmpty(result.msg)) {
					ToastAlone.showToast(mActivity,com.fanfanwang.ffw.app.AppConstant.URLDecoderString(result.msg));
				} else {
					ToastAlone.showToast(mActivity,mActivity.getResources().getString(R.string.e_net_error));
				}

			}
		}
	}
}
